package com.wish.contract.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.wish.common.security.utils.SecurityUtils;
import com.wish.contract.entity.TCwPaymentPlan;
import com.wish.contract.entity.vo.ContractVO;
import com.wish.contract.mapper.TCwPaymentPlanMapper;
import com.wish.contract.service.ITCwPaymentPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 高锦涛 付款计划表 服务实现类
 * </p>
 *
 * @author cjl
 * @since 2022-03-27
 */
@Service
public class TCwPaymentPlanServiceImpl extends ServiceImpl<TCwPaymentPlanMapper, TCwPaymentPlan> implements ITCwPaymentPlanService {

    @Override
    public List<TCwPaymentPlan> getPaymentPlans(ContractVO contractVO) {
        //付款单集合
        List<TCwPaymentPlan> tCwPaymentPlans = new ArrayList<TCwPaymentPlan>();

        //房产押金
        BigDecimal depositMoney = contractVO.getDepositMoney();
        //押几
        Integer depositType = contractVO.getDepositType();
        //总押金
        BigDecimal multiply = depositMoney.multiply(new BigDecimal(depositType));

        //月租金
        BigDecimal monthRent = contractVO.getMonthRent();
        //付几
        Integer paymentType = contractVO.getPaymentType();
        //租金
        BigDecimal multiply1 = monthRent.multiply(new BigDecimal(paymentType));

        Date leaseStartTime = contractVO.getLeaseStartTime();
        //押金账单
        if (!multiply.equals(BigDecimal.ZERO)) {
            TCwPaymentPlan tCwPaymentPlan = new TCwPaymentPlan();
            //费用类型81押金
            tCwPaymentPlan.setItemType(81);
            //付款类型
            //,1:收房付款单 2：租务付款单 3：解约结算付款 4：续约结算付款
            tCwPaymentPlan.setContractType(1);
            //付款状态
            //,"1、未付款；2、已付款
            //3、已中止"4、 4：已冻结（未付款的时候，才会变成已冻结，所以恢复冻结状态的时候，是恢复成未收款状态）
            tCwPaymentPlan.setPayType(1);

            tCwPaymentPlan.setPayState(1);
            //账单类型（金融属性）,"1：收房贷变更
            // 2：退租变更
            // 3：普通账单(正常收房租务计划) 4：续约结算产生的"
            tCwPaymentPlan.setBillType(3);
            //款项（费用类型）,1:水费 2：电费，3：煤/燃气费，80：租金，81：押金，90：解约结算，99:其它
            tCwPaymentPlan.setItemType(80);
            tCwPaymentPlan.setPaymentAmount(multiply);
            //税费
            tCwPaymentPlan.setTax(new BigDecimal(0));
            //序号
            tCwPaymentPlan.setPayNumber(0);
            //开始
            tCwPaymentPlan.setPaymentDaysBegin(leaseStartTime);
            //应付日期
            tCwPaymentPlan.setPayableDate(leaseStartTime);
            tCwPaymentPlan.setPaymentDaysEnd(DateUtil.offsetDay(DateUtil.offsetMonth(leaseStartTime, 1), -1));
            tCwPaymentPlan.setCreateUserid(SecurityUtils.getUserId());
            tCwPaymentPlans.add(tCwPaymentPlan);
        }


        //租金账单
        for (int i = 1; ; i++) {
            TCwPaymentPlan tCwPaymentPlan = new TCwPaymentPlan();
            //费用类型80租金
            tCwPaymentPlan.setItemType(80);
            tCwPaymentPlan.setContractType(1);
            tCwPaymentPlan.setPayType(1);
            //付款状态
            tCwPaymentPlan.setPayState(1);
            tCwPaymentPlan.setBillType(3);
            //序号
            tCwPaymentPlan.setPayNumber(i);
            //税费
            tCwPaymentPlan.setTax(new BigDecimal(0));
            tCwPaymentPlan.setPaymentDaysBegin(leaseStartTime);
            //应付日期
            tCwPaymentPlan.setPayableDate(leaseStartTime);
            //开始日期变化
            leaseStartTime = DateUtil.offsetMonth(leaseStartTime, paymentType);
            DateTime dateTime = DateUtil.offsetDay(leaseStartTime, -1);
            tCwPaymentPlan.setCreateUserid(SecurityUtils.getUserId());
            //最后一期结束日期>=总结束日期, 结束循环
            if (DateUtil.compare(leaseStartTime, contractVO.getLeaseEndTime()) >= 0) {
                //获取月份差值(不足一个月不计算)
                double floor = Math.floor((DateUtil.dayOfYear(dateTime) - DateUtil.dayOfYear(contractVO.getLeaseEndTime())) / 30.0);
                tCwPaymentPlan.setPaymentAmount(monthRent.multiply(new BigDecimal(paymentType - floor)));
                tCwPaymentPlan.setPaymentDaysEnd(contractVO.getLeaseEndTime());
                tCwPaymentPlans.add(tCwPaymentPlan);
                break;
            }
            tCwPaymentPlan.setPaymentAmount(multiply1);
            tCwPaymentPlan.setPaymentDaysEnd(dateTime);
            tCwPaymentPlans.add(tCwPaymentPlan);
        }
        return tCwPaymentPlans;
    }
}
